<div class="amp-content">
  <h3 class="amp-header"><?php echo $action_title; ?></h3>

  <div class="amp-content">
    <?php if ($path || ! empty($filters) || (! empty($pager) && $pager->getTotalPages() > 1)) { ?>
    <form action="<?php echo s::makeUrl($route) ?>" method="get">
      <span class="toolbar-container">
        <ul class="toolbar">
          <li class="toolbar-item"><a href="<?php echo s::makeUrl($route, array($parent => $path[count($path) - 1][$parent])) ?>">&uarr; up</a></li>

          <li class="toolbar-item">
            <a href="<?php echo s::makeUrl($route, array($parent => 0)) ?>">Root</a>

            <?php if ($path) {
              $c = count($path);
              foreach ($path as $_parent) { ?>
                &ndash;
                <?php if (--$c > 0) { ?>
                  <a href="<?php echo s::makeUrl($route, array($parent => $_parent[$pk])) ?>"><?php echo $_parent[$label] ?></a>
                <?php } else {
                  echo $_parent[$label];
                }
              }
            }
            ?>
          </li>

          <?php if (! empty($filters)) { ?>
          <li class="toolbar-item">Filter</li>

          <?php foreach ($filters as $filter) { ?>
          <li class="toolbar-item"><?php echo $filter->render() ?></li>
          <?php } ?>

          <li class="toolbar-item"><input type="submit" value="Ok"/></li>
          <?php } ?>

          <?php if (! empty($pager)) { ?>
            <?php echo $this->pager->show($pager); ?>
          <?php } ?>
        </ul>
        <div style="clear:both;"></div>
      </span>
    </form>
    <?php } ?>

    <form action="<?php echo s::makeUrl($route, array('action' => 'bulk')) ?>" method="post" class="wrapper">
      <table class="amp-table wrapped">
        <?php if ($has_groups) { ?>
        <tr>
          <?php foreach ($groups as $span) { ?>
          <th colspan="<?php echo $span[0] ?>"><?php echo $span[1] ?></th>
          <?php } ?>
        </tr>
        <?php } ?>

        <tr>
          <th style="width: 1%;"></th>

          <?php
          while ($element = $elements->next()) {
            if ($element instanceof FormGroup) {
              $_elements = $element->getElements($action);

              while ($_element = $_elements->next()) {
                echo '<th>' . $_element->getTitle() . '</th>';
              }
            }
            else {
              echo '<th>' . $element->getTitle() . '</th>';
            }
          }
          ?>

          <th>Actions</th>
        </tr>

        <tbody>
        <?php while ($obj = $data->next()) { ?>
        <tr class="<?php echo $this->cycle->next('default', array('odd', 'even')) ?>">
          <td><input type="checkbox" class="row-check" name="<?php echo $pk ?>[]" value="<?php echo $obj->$pk ?>"/></td>

          <?php $elements->reset(); ?>

          <?php
          while ($element = $elements->next()) {
            if ($element instanceof FormGroup) {
              $_elements = $element->getElements($action);

              while ($_element = $_elements->next()) { ?>
                <td><?php echo $_element->render($obj, $action) ?></td>
              <?php }
            } else { ?>
              <td><?php echo $element->render($obj, $action) ?></td>
            <?php }
          } ?>

          <td class="no-label no-wrap" style="text-align:center; width:1%;">
            <?php
            if ($sortable) {
              echo $this->menu_item->show(array('label' => 'First', 'icon' => 'top', 'route' => $route, 'params' => array('formAction' => 'sort', $pk => $obj->$pk, 'sortIndex' => 'top')));
              echo $this->menu_item->show(array('label' => 'Previous', 'icon' => 'up', 'route' => $route, 'params' => array('formAction' => 'sort', $pk => $obj->$pk, 'sortIndex' => 'up')));
              echo $this->menu_item->show(array('label' => 'Next', 'icon' => 'down', 'route' => $route, 'params' => array('formAction' => 'sort', $pk => $obj->$pk, 'sortIndex' => 'down')));
              echo $this->menu_item->show(array('label' => 'Last', 'icon' => 'bottom', 'route' => $route, 'params' => array('formAction' => 'sort', $pk => $obj->$pk, 'sortIndex' => 'bottom')));
            }

            echo $this->menu_item->show(array('icon' => 'view', 'label' => 'View', 'route' => $route, 'params' => array('action' => 'view', $pk => $obj->$pk)));
            echo $this->menu_item->show(array('icon' => 'edit', 'label' => 'Edit', 'route' => $route, 'params' => array('action' => 'edit', $pk => $obj->$pk)));
            echo $this->menu_item->show(array('label' => 'Delete', 'icon' => 'delete', 'route' => $route,
              'params' => array('action' => 'delete', $pk => $obj->$pk), 'linkParams' => array('onclick' => "return confirm('Do you confirm this action?');")));
            ?>
          </td>
        </tr>
        <?php } ?>
        </tbody>
      </table>

      <span class="toolbar-container">
        <ul class="toolbar">
          <li class="toolbar-item">
            <select name="bulk_action">
              <option value="">Bulk actions</option>
              <option value="view">View</option>
              <option value="delete">Delete</option>
            </select>
            <input type="submit" value="Ok"/>
          </li>

          <li class="toolbar-item no-label">
            Selecionar
            <a href="javascript:" onclick="selectAll();"><span class="icon icon-select-all"></span><span class="label">Select all</span></a>
            <a href="javascript:" onclick="selectNone();"><span class="icon icon-select-none"></span><span class="label">Select none</span></a>
            <a href="javascript:" onclick="selectInvert();"><span class="icon icon-select-invert"></span><span class="label">Select invert</span></a>
          </li>

          <?php if (! empty($pager)) { ?>
            <?php echo $this->pager->show($pager); ?>
          <?php } ?>
        </ul>

        <div style="clear:both;"></div>
      </span>
    </form>

    <script>
    function selectAll() {
      $('.row-check').attr('checked', 'checked');
    };

    function selectNone() {
      $('.row-check').removeAttr('checked');
    };

    function selectInvert() {
      var on = $('.row-check:checked');
      $('.row-check').attr('checked', 'checked');
      on.removeAttr('checked');
    };

    <?php if ($sortable) { ?>
    $(document).ready(function() {
      $('.amp-table tbody').sortable({
        axis: 'y',

        forcePlaceholderSize: true,

        stop: function(event, ui) {
          $('.amp-table tbody tr').each(function(index, item) {
            $(item).removeClass('odd').removeClass('even').addClass(index % 2 == 0 ? 'even' : 'odd');
          });

          var url = '<?php echo s::makeUrl($route, array('formAction' => 'sort')) ?>';

          var data = {
            sortIndex: ui.item.index() + 1,
            <?php echo $pk ?>: ui.item.find('.row-check').val()
          };

          $.get(url, data);
        }
      });
    });
    <?php } ?>
    </script>
  </div>
</div>